//
//  ClassRoomViewController.swift
//  scfriend
//
//  Created by wulanzhou on 2017/7/26.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

import UIKit

class ClassRoomViewController: BaseViewController,UIScrollViewDelegate {
    
    //MAKE:懒加载
    private lazy var pageTab:PageTabBar = {
    
        let tab = PageTabBar.init(frame: CGRect(x: 0, y: 0, width: 120, height: 35.0))
        tab.tabBarSelectedItemIndexBlock = { (_ index:Int) in
            self.selectedTab(index: index,animated: true)
        }
        return tab
    
    }()
    
    private lazy var pageScrollView:UIScrollView = {
        
        let page = UIScrollView.init(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height))
        page.delegate = self
        page.isPagingEnabled = true
        page.isUserInteractionEnabled = true
        page.bounces = false
        page.showsHorizontalScrollIndicator = false
        page.showsVerticalScrollIndicator = false
        page.contentSize = CGSize(width: self.view.width * 2, height: page.height)
        
        return page
    
    }()
    
    private var controllerList:NSMutableArray = {
        
        let mul = NSMutableArray.init(capacity: 0)
        mul.add(NSNull.init())
        mul.add(NSNull.init())
        return mul
        
    }()
    
    //MAKE：变量定义
    private var isUseDragging:Bool = false //是否使用手拖动的，自动的就设置为NO
    private var isEndDecelerating:Bool = false
    private var startOffsetX:CGFloat = 0
    private var continueDraggingNumber:UInt = 0
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.tabBarController?.navigationItem.titleView = self.pageTab
        //self.navigationItem.titleView = self.pageTab
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
       
        self.view.addSubview(self.pageScrollView)
        
        self.changeToController(0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    private func selectedTab(index:Int,animated:Bool)  {
        self.pageTab.selectTab(index: index, animate: animated,completion: nil)
        self.pageScrollView.setContentOffset(CGPoint(x: CGFloat(index)*self.pageScrollView.width, y: 0), animated: animated)
        self.isUseDragging = false
        self.changeToController(index)
    }
    
    private func changeToController(_ index:Int){
        
        if (self.controllerList[index] as AnyObject).isKind(of:NSNull.self) {
            let vc = SmallGroupViewController()
            vc.view.frame = CGRect(x: CGFloat(index)*self.pageScrollView.width, y: 0, width: self.pageScrollView.width, height: self.pageScrollView.height)
            vc.view.backgroundColor = UIColor.randomColor()
            self.pageScrollView.addSubview(vc.view)
            self.controllerList[index] = vc
        }
    
    }
    
    // mark - ScrollView Delegate
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        
        if scrollView == self.pageScrollView {
        
            self.continueDraggingNumber += 1
            if (self.isEndDecelerating) {
                self.startOffsetX = scrollView.contentOffset.x
            }
            self.isUseDragging = true
            self.isEndDecelerating = false
        }
        
    }
    //对拖动过程中的处理
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        if scrollView == self.pageScrollView {
        
            //let movingOffsetX:CGFloat = scrollView.contentOffset.x - self.startOffsetX;
            if (self.isUseDragging) {
                //tab处理事件待完成
                //[self moveSelectedLineByScrollWithOffsetX:movingOffsetX];
            }
        }
        
    }
    //手释放后pager归位后的处理
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == self.pageScrollView {
        
            let index = (Int)(scrollView.contentOffset.x/self.pageScrollView.width)
            self.selectedTab(index: index, animated: true)
            //[self selectTabWithIndex:(int)scrollView.contentOffset.x/self.bounds.size.width animate:YES];
            self.isUseDragging = true
            self.isEndDecelerating = true
            self.continueDraggingNumber = 0
        
        }
    }
    //自动停止
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        if scrollView == self.pageScrollView {
            let index = (Int)(scrollView.contentOffset.x/self.pageScrollView.width)
            self.selectedTab(index: index, animated: true)
        }
    }

  

}
